package com.sinoglobal.networkconnections;

import android.support.annotation.NonNull;

import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.Map;

import okhttp3.OkHttpClient;

/**
 * Created by XINDZ on 2017/1/19.
 * 功能:网络请求帮助类
 */

public class HttpTools {
    private static final byte[] LOCKER = new byte[0];
    private static HttpTools mInstance;


    public static HttpTools getInstance() {
        if (mInstance == null) {
            synchronized (LOCKER) {
                if (mInstance == null) {
                    mInstance = new HttpTools();
                }
            }
        }
        return mInstance;
    }


    /**
     * 基础GET请求
     *
     * @param tag
     * @param url   请求URL
     * @param param 参数
     */
    public RequestCall get(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> param) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param);
        RequestCall build = OkHttpUtils
                .get()
                .url(url)
                .tag(tag)
                .params(param)
                .build();
        return build;
    }

    /**
     * 基础GET请求
     *
     * @param tag
     * @param url 请求URL
     */
    public RequestCall get(@NonNull Object tag, @NonNull String url) {
        Logger.i("HTTPTOOLS--Url:" + url);
        RequestCall build = OkHttpUtils
                .get()
                .tag(tag)
                .url(url)
                .build();
        return build;
    }

    /**
     * GET请求带header
     *
     * @param tag
     * @param url          请求URL
     * @param headerParams 请求头
     * @param param        参数
     */
    public RequestCall getHeader(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> headerParams, @NonNull Map<String, String> param) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param + "&HeaderParams:" + headerParams);
        RequestCall build = OkHttpUtils
                .get()
                .tag(tag)
                .url(url)
                .headers(headerParams)
                .params(param)
                .build();
        return build;
    }

    /**
     * GET请求带header
     *
     * @param tag
     * @param url          请求URL
     * @param headerParams 请求头
     */
    public RequestCall getHeader(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> headerParams) {
        Logger.i("HTTPTOOLS--Url:" + url + "&HeaderParams:" + headerParams);
        RequestCall build = OkHttpUtils
                .get()
                .tag(tag)
                .url(url)
                .headers(headerParams)
                .build();
        return build;
    }

    /**
     * 基础GET请求
     *
     * @param tag
     * @param url   请求URL
     * @param param 参数
     */
    public RequestCall get(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> param, String message) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param );
        RequestCall build = OkHttpUtils
                .get()
                .url(url)
                .tag(tag)
                .params(param)
                .build();
        return build;
    }

    /**
     * GET请求自定义OkHttpClient
     *
     * @param tag
     * @param url          请求URL
     * @param okHttpClient 自定义Okhttpclient
     */
    public RequestCall getClient(@NonNull Object tag, @NonNull String url, @NonNull OkHttpClient okHttpClient) {
        Logger.i("HTTPTOOLS--Url:" + url );
        RequestCall build = OkHttpUtils
                .initClient(okHttpClient)
                .get()
                .tag(tag)
                .url(url)
                .build();
        return build;
    }

    /**
     * 基础Post请求
     *
     * @param tag
     * @param url   请求URL
     * @param param 参数
     */
    public RequestCall post(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> param) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param );
        RequestCall build = OkHttpUtils
                .post()
                .tag(tag)
                .url(url)
                .params(param)
                .build();
        return build;
    }


    /**
     * Post请求带header
     *
     * @param tag
     * @param url          请求URL
     * @param headerParams 请求头
     * @param param        参数
     */
    public RequestCall postHeader(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> headerParams, @NonNull Map<String, String> param) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param + "&HeaderParams:" + headerParams);
        RequestCall build = OkHttpUtils
                .post()
                .tag(tag)
                .url(url)
                .headers(headerParams)
                .params(param)
                .build();
        return build;
    }

    /**
     * Post请求 自定义OKhttpclient
     *
     * @param tag
     * @param url          请求URL
     * @param param        参数
     * @param okHttpClient 自定义Okhttpclient
     */
    public RequestCall postClient(@NonNull Object tag, @NonNull String url, @NonNull Map<String, String> param, @NonNull OkHttpClient okHttpClient) {
        Logger.i("HTTPTOOLS--Url:" + url + "&Param:" + param );
        RequestCall build = OkHttpUtils
                .initClient(okHttpClient)
                .post()
                .tag(tag)
                .url(url)
                .params(param)
                .build();
        return build;
    }

    /**
     * 取消网络请求
     *
     * @param call
     */
    public void httpCancel(RequestCall call) {
        if (call != null) {
            call.cancel();
        }
    }
}
